package com.ejd.web.vo.manage.goods;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.faces.application.FacesMessage;
import javax.faces.model.SelectItem;

import net.sf.cglib.beans.BeanCopier;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;

import org.richfaces.component.UIDataTable;

import com.ejd.service.IApplyService;
import com.ejd.service.ICategoryService;
import com.ejd.service.IGoodsService;
import com.ejd.service.IProductUnitService;
import com.ejd.service.IStakeholderService;
import com.ejd.service.ISubCategoryService;
import com.ejd.utils.MessageFactory;
import com.ejd.utils.SpringFacesUtil;
import com.ejd.utils.UIComponentUtil;
import com.ejd.utils.ValidatorUtil;
import com.ejd.web.bo.impl.Apply;
import com.ejd.web.bo.impl.Category;
import com.ejd.web.bo.impl.Goods;
import com.ejd.web.bo.impl.GoodsProperties;
import com.ejd.web.bo.impl.Productunit;
import com.ejd.web.bo.impl.Stakeholder;
import com.ejd.web.bo.impl.SubCategory;
import com.ejd.web.constant.EjdConstants;
import com.ejd.web.constant.ManageBeanConstants;
import com.ejd.web.constant.SpringBeanConstants;
import com.ejd.web.vo.common.GoodsPropertiesVo;
import com.ejd.web.vo.common.GoodsVo;
import com.ejd.web.vo.genl.ExistSubCategoryBean;

public class UpdateGoodsPageBean extends GoodsCrudBean implements java.io.Serializable{

	private static final long serialVersionUID = 5054087821033164852L;
	//create or update shoes properties

	//end
	public UpdateGoodsPageBean(){
		
	}

	
	/*public String saveGoods() {
		this.setDisplayMessage(null);
		IGoodsService<Goods,Long> goodsService = (IGoodsService<Goods,Long>)SpringFacesUtil.getSpringBean(SpringBeanConstants.GOODS_SERVICE_BEAN_NAME);
		goodsService.setEntityClass(Goods.class);
		goodsService.setPKClass(Long.class);
		boolean hasGoods = goodsService.ifExistBy("code", this.getGoods().getCode().trim());
		if (hasGoods) {
			String message = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"operate.save.fail.hasexist.display", FacesMessage.SEVERITY_ERROR).getDetail();
			this.setDisplayMessage(message);
			return null;
		}
		validateGoods(this.getGoods(),this.getErrorMessages());
		if (!(this.getErrorMessages().isEmpty())) {
			return null;
		}
		Goods goods = new Goods();
		BeanCopier copyHere = BeanCopier.create(GoodsVo.class,Goods.class,false);
		copyHere.copy(this.getGoods(), goods, null);
		//start set product unit
		IProductUnitService<Productunit,Long> productUnitService = (IProductUnitService<Productunit,Long>)SpringFacesUtil.getSpringBean(SpringBeanConstants.PRODUCTUNIT_SERVICE_BEAN_NAME);
		productUnitService.setEntityClass(Productunit.class);
		productUnitService.setPKClass(Long.class);
		Productunit theUnit = productUnitService.findUniqueBy("unitId", this.getGoods().getUnitv().getUnitId());
		goods.setUnit(theUnit);
		//end set product unit
		//start set stake holder
		IStakeholderService<Stakeholder,Long> stakeholderService = (IStakeholderService<Stakeholder,Long>)SpringFacesUtil.getSpringBean(SpringBeanConstants.STAKEHOLDER_SERVICE_BEAN_NAME);
		stakeholderService.setEntityClass(Stakeholder.class);
		stakeholderService.setPKClass(Long.class);
		Stakeholder theStakeholder = stakeholderService.findUniqueBy("stakeholderId", this.getGoods().getProviderv().getStakeholderId());
		goods.setProvider(theStakeholder);
		//end set stake holder
		//start set properties
		List<GoodsPropertiesVo> tempGoodsProperties = this.getGoods().getPropertiesv();
		if (null != tempGoodsProperties && !(tempGoodsProperties.isEmpty())) {
			Set<GoodsProperties> properties = new HashSet<GoodsProperties>();
			BeanCopier copyIn = BeanCopier.create(GoodsPropertiesVo.class, GoodsProperties.class, false);
			for (GoodsPropertiesVo propertiesVo:tempGoodsProperties) {
				GoodsProperties tempProperties = new GoodsProperties();
				copyIn.copy(propertiesVo, tempProperties, null);
				properties.add(tempProperties);
			}
			goods.setProperties(properties);
		}
		//end set properties
		
		goodsService.save(goods);
		String message = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"operate.save.success.display", FacesMessage.SEVERITY_ERROR).getDetail();
		this.setDisplayMessage(message);
		
		return null;
	}*/
	
	public String preNewGoodsProperties(){
		UpdateGoodsPropertiesPageBean updateGoodsPropertiesPage = (UpdateGoodsPropertiesPageBean)SpringFacesUtil.getManagedBean(ManageBeanConstants.UPDATE_GOODS_PROPERTIES_PAGE_BEAN_NAME);
		updateGoodsPropertiesPage.setActionOfProperties(EjdConstants.ACTION_NEW);
		updateGoodsPropertiesPage.setErrorMessages(null);
		GoodsPropertiesVo newVo = new GoodsPropertiesVo();
		newVo.setColor("");
		newVo.setSize("");
		newVo.setNumber(0);
		updateGoodsPropertiesPage.setGoodsPropertiesVo(newVo);
		return null;
	}
	public String preUpdateGoodsProperties(){
		Integer selectedRow = Integer.valueOf((String)SpringFacesUtil.getRequestParameter("selectedRow"));
		Map<String,UIComponent> componentMap = new HashMap<String,UIComponent>();
		List<UIComponent> te= new ArrayList<UIComponent>();
		UIViewRoot currentViewRoot = FacesContext.getCurrentInstance().getViewRoot();
		for(UIComponent component : currentViewRoot.getChildren()){ 
            te= UIComponentUtil.getComponentChildren(component,componentMap); 
        }
		UIDataTable tempTable = (UIDataTable) componentMap.get("table");
		this.setPropertiesDataTable(tempTable);
		GoodsPropertiesVo editData = (GoodsPropertiesVo)tempTable.getRowData();
		UpdateGoodsPropertiesPageBean updateGoodsPropertiesPage = (UpdateGoodsPropertiesPageBean)SpringFacesUtil.getManagedBean(ManageBeanConstants.UPDATE_GOODS_PROPERTIES_PAGE_BEAN_NAME);
		updateGoodsPropertiesPage.setActionOfProperties(EjdConstants.ACTION_EDIT);
		if (null != this.getErrorMessages() && this.getErrorMessages().size() > 0) {
			this.getErrorMessages().clear();
		}
		updateGoodsPropertiesPage.setSelectedRow(selectedRow);
		GoodsPropertiesVo originalGoodsPropertiesVo = new GoodsPropertiesVo();
		BeanCopier copyHere = BeanCopier.create(GoodsPropertiesVo.class,GoodsPropertiesVo.class,false);
		copyHere.copy(editData, originalGoodsPropertiesVo, null);
		updateGoodsPropertiesPage.setGoodsPropertiesVo(originalGoodsPropertiesVo);
		return null;
	}
	
	
	
}
